﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;

namespace MMOG
{
    public class RawImage
    {
        public byte[] buff;
        public int width;
        public int height;

        public RawImage(String filename, uint transparent)
        {
            loadBmp(filename, transparent);
        }

        public void loadBmp(String fileName, uint transparent)
        {
            Bitmap bitmap = new Bitmap(fileName);
            width = bitmap.Width;
            height = bitmap.Height;
            buff = new byte[width * height * 4];
            for (int x = 0; x < width; x++)
                for (int y=0; y < height; y++)
                {
                    Color color = bitmap.GetPixel(x,height-y-1);
                    uint temp = (uint)(color.R + color.G * 256 + color.B * 65536);
                    buff[(y * width + x) * 4] = color.R;
                    buff[(y * width + x) * 4 + 1] = color.G;
                    buff[(y * width + x) * 4 + 2] = color.B;
                    
                    if (temp == transparent)
                        buff[(y * width + x) * 4 + 3] = 0;
                    else
                        buff[(y * width + x) * 4 + 3] = 255;
                }
        }

        public void draw(float x, float y)
        {
            OpenGLControl.glRasterPos2f(x, y);
            OpenGLControl.glBlendFunc(OpenGLControl.GL_SRC_ALPHA, OpenGLControl.GL_ONE_MINUS_SRC_ALPHA);
            unsafe
            {
                fixed (byte* pBuff = buff)
                    OpenGLControl.glDrawPixels(width, height, OpenGLControl.GL_RGBA, OpenGLControl.GL_UNSIGNED_BYTE, pBuff);
            }
        }
    }
}